/**
 * @Description:
 * @author 若琳
 * @date 2021/7/1
*/


const {MYSQL}  = require("../../../../../../../../ruoling-common/src/com/ruoling/common/config/mysqlconfig.js");
const Pager_DB = require("../../../../../../../../ruoling-common/src/com/ruoling/common/table/Pager_DB.js");
const md5 = require("md5-node");

module.exports = class UserService{

    /**
     *
     * @param limit
     * @param page
     * @param sys_loginname
     * @param sys_name
     * @param sys_admin_type
     * @param callback
     * @constructor
     */
     static async User_List(limit,page,sys_loginname,sys_name,sys_admin_type,callback){

        try{

            let pagerdb = await new Pager_DB(limit,page).table("rl_sys_user")
                .and({"key":"sys_loginname","value":sys_loginname})
                .and({"key":"sys_name","value":sys_name})
                .and({"key":"sys_admin_type","value":sys_admin_type}).select();

            callback(null,pagerdb);
            return;
        }catch (e){

            callback(e);
            return;
        }
    };

     static async UserAddService(sys_loginname,sys_loginpwd,sys_name,rolearr_id,callback){
         let startTrans = null;
       try{
           let connect = await MYSQL().connect();
           startTrans = await connect.startTrans();
           let user_obj = {
               sql:"INSERT INTO rl_sys_user(sys_loginname,sys_loginpwd,sys_name,sys_user_type,sys_user_sex,sys_admin_type,sys_endtime,sys_pwd_update_date,Create_time)VALUES(?,?,?,?,?,?,?,?,?)",
               param:[sys_loginname,md5(sys_loginpwd),sys_name,"注册用户",0,0,new Date(),new Date(),new Date()]
           };
          let user_id = await startTrans.query(user_obj.sql,user_obj.param);

          let role_user = {
              sql:"insert into rl_sys_user_role(sys_user_id,sys_role_id,Create_time)values ?",
              param:[]
          };

           for(let i = 0;i<rolearr_id.length;i++){
               let arr = [user_id.insertId,rolearr_id[i],new Date()];
               role_user.param.push(arr);
           }

           let sys_role_user = await startTrans.query(role_user.sql,[role_user.param]);
           await startTrans.commit();
           callback(null,sys_role_user.affectedRows);
           return;
       }catch (e){
           await startTrans.rollback();
           callback(e);
           return;
       }
     };

    /**
     * 批量删除Service
     * @param Id_all
     * @param callback
     * @returns {Promise<void>}
     * @constructor
     */
     static async UserDeleteAll(Id_all,callback){
        let startTrans = null;
         try{
             let connect = await MYSQL().connect();
             startTrans = await connect.startTrans();
             let obj = {
                 sql:"update rl_sys_user set Is_delete = 0 where id in("+Id_all.join(",")+")"
             }
             let row = await startTrans.query(obj.sql);
             await startTrans.commit();
             callback(null,row.affectedRows);
             return;
         }catch (e){
             await startTrans.rollback();
             callback(e);
             return;
         }
     };

    /**
     * 获取单个用户的角色ID
     * @param Id
     * @param callback
     * @returns {Promise<void>}
     * @constructor
     */
     static async UserRoleIdAll(Id,callback){
         let connect = null;
         try{

             connect = await MYSQL().connect();
             let obj = {
                 sql:"SELECT rl_sys_role_id FROM sys_user_role WHERE sys_user_id = "+Id+" and Is_delete = 1"
             };

             let user_obj = {
                 sql:"SELECT * FROM rl_sys_user WHERE ID = "+Id+" and Is_delete = 1"
             };
             let User = await connect.connect_query(user_obj.sql);
             let list = await connect.connect_query_close(obj.sql);
            let arr = [];
             for (let i = 0;i<list.length;i++){
                arr.push(list[i].sys_role_id);
             }

             callback(null, {user:User[0],list:arr});
             return ;
         }catch (e){

            if(connect){
                connect.close();
            }

            callback(e);
            return ;
         }
     };

    /**
     * 用户修改Service
     * @param sys_loginname
     * @param sys_name
     * @param rolearr_id
     * @param ID
     * @param callback
     * @returns {Promise<void>}
     * @constructor
     */
     static async UserEditService(sys_loginname,sys_name,rolearr_id,ID,callback){

         let startTrans = null;
         try{
             let connect = await MYSQL().connect();
             startTrans = await connect.startTrans();
             let role_obj = {
                 sql:"UPDATE rl_sys_user SET sys_loginname = \""+sys_loginname+"\",sys_name = \""+sys_name+"\" WHERE ID = "+ID+"",
             };
             await startTrans.query(role_obj.sql);
             let user_role_obj = {
                 sql:"UPDATE rl_sys_user_role SET Is_delete = 0 WHERE sys_user_id = "+ID+""
             };
             await startTrans.query(user_role_obj.sql);
             let role_user_insert_obj = {
                 sql:"insert into rl_sys_user_role(sys_user_id,sys_role_id,Create_time)values ?",
                 param:[]
             };
             for(let i = 0;i<rolearr_id.length;i++){
                 let arr = [ID,rolearr_id[i],new Date()];
                 role_user_insert_obj.param.push(arr);
             }

             let sys_role_user = await startTrans.query(role_user_insert_obj.sql,[role_user_insert_obj.param]);

             await startTrans.commit();
             callback(null,sys_role_user.affectedRows);
             return;

         }catch (e){
             await startTrans.rollback();
             callback(e);
             return ;
         }
     }

}